package schedule.model;

import java.util.ArrayList;

public class Student {

	public String bannerID;
	public String email;
	public ArrayList<InputTime> availableTimes;
	public ArrayList<InputTime> compatibleTimes;
	public MilTimeSchedule monMil;
	public MilTimeSchedule tueMil;
	public MilTimeSchedule wedMil;
	public MilTimeSchedule thuMil;
	public MilTimeSchedule friMil;
	public MilTimeSchedule monCom;
	public MilTimeSchedule tueCom;
	public MilTimeSchedule wedCom;
	public MilTimeSchedule thuCom;
	public MilTimeSchedule friCom;
	private ArrayList<InputTime> myCompatTimes;
	
	//make milTimeSchedule for each day
	
	public Student(String bannerID){
		this.bannerID = bannerID;
		availableTimes = new ArrayList<InputTime>();
		compatibleTimes = new ArrayList<InputTime>();
		//myCompatTimes = new ArrayList<InputTime>();
		initialize();
	}
	public void initialize(){		
		monMil = new MilTimeSchedule();
		tueMil = new MilTimeSchedule();
		wedMil = new MilTimeSchedule();
		thuMil = new MilTimeSchedule();
		friMil = new MilTimeSchedule();
		monCom = new MilTimeSchedule();
		tueCom = new MilTimeSchedule();
		wedCom = new MilTimeSchedule();
		thuCom = new MilTimeSchedule();
		friCom = new MilTimeSchedule();
	}
	//possible place to check if time is already added?
	public void addTime(InputTime newTime){
		availableTimes.add(newTime);
	}
	public void createSchedules(){
		createMyMilTime();		
	}
	//goes through every input time in instance's availableTimes to set according milTime
	private void createMyMilTime(){
		//appends hr and min together, as milTimeSchedule expects. i.e. sHr= 10 sMin= 45 turns into 1045
		
		for (InputTime a: availableTimes){
			StringBuffer sBuffer= new StringBuffer();
			//Should be coming back as milTimes
			sBuffer.append(a.startHr);
			sBuffer.append(a.startMn);
			int startTime = Integer.parseInt(sBuffer.toString());
			if (a.startMn == 0) startTime = startTime*10;
			sBuffer.delete(0, sBuffer.length());
			sBuffer.append(a.endHr);
			sBuffer.append(a.endMn);
			int endTime = Integer.parseInt(sBuffer.toString());
			if (a.endMn == 0) endTime = endTime*10;
			int dayNum = a.currentDay;
			switch(dayNum){
				case 0:
					monMil.setAvailable(startTime, endTime);
					break;
				case 1:
					tueMil.setAvailable(startTime, endTime);
					break;
				case 2:
					wedMil.setAvailable(startTime, endTime);
					break;
				case 3:
					thuMil.setAvailable(startTime, endTime);
					break;
				case 4:
					friMil.setAvailable(startTime, endTime);
					break;
			}
		}
	}	
	public ArrayList<InputTime> compatInputTimes(){		
		//convert all available times in compatible milTime back to inputTimes
		addTimesForEachDay(monCom, 0);
		addTimesForEachDay(tueCom, 1);
		addTimesForEachDay(wedCom, 2);
		addTimesForEachDay(thuCom, 3);
		addTimesForEachDay(friCom, 4);
		return compatibleTimes;
	}
	//converts available freeslots in resulting Miltime into inputTimes and adds them to ArrayList
	private void addTimesForEachDay(MilTimeSchedule indDay, int day){
		int i =0;
		StringBuffer sb = new StringBuffer();
		while (i<indDay.freeSlot){
			Integer startTime = indDay.getStart(i);
			Integer endTime = indDay.getEnd(i);
			
		//	System.out.println(bannerID + " is compatible at " + indDay.getStart(i) + " to " +indDay.getEnd(i) + " on " + day);
			//needed to use StringBuilder to break integer up, e.g. turn 1045 into startHr 10 startMin 45
			String startString = startTime.toString();
			
			sb.append(startString);	
			String startHrString, startMinString;
			//if startTime is 3 digits, e.g 900
			if (startString.length() == 3){
				startHrString = sb.substring(0, 1);
				startMinString = sb.substring(1, 3);
			}else{ //startTime is a full 4 digits
				startHrString = sb.substring(0, 2);
				startMinString = sb.substring(2, 4);
			}
			int startHr = Integer.parseInt(startHrString);
			int startMn = Integer.parseInt(startMinString);	
			sb.delete(0, sb.length());
			String endString = endTime.toString();	
			sb.append(endString);	
			String endHrString, endMinString;
			if (endString.length() == 3){
				endHrString = sb.substring(0, 1);
				endMinString = sb.substring(1, 3);
			}else{
				endHrString = sb.substring(0, 2);
				endMinString = sb.substring(2, 4);
			}
			int endHr = Integer.parseInt(endHrString);
			int endMn = Integer.parseInt(endMinString);	
			//empty stringbuilder
			sb.delete(0, sb.length());
			//System.out.println("new input time of startHr " + startHr + " startMin" + startMn + " endHr " + endHr + " endMn " + endMn);
			InputTime t = new InputTime(startHr, startMn, endHr, endMn, day);
			compatibleTimes.add(t);
			i++;
		}
	}
}
